<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {

	/**
	 * 用户注册页面
	 */
	public function index()
	{
		if($this->session->userdata('user_id'))
		{
			header('location:'.base_url().'ucenter');
		}
		$data = array('title'=>'用户注册 - 作业本教材注册','h_title'=>'欢迎注册','style'=>'register/register');
		
		$this->load->view('header_980',$data);
		$this->load->view('register/register');
		$this->load->view("footer/simplefooter");
	}
	
	/**
	 * 处理用户注册数据
	 * 
	 */
	public function registerAct()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('m_user');
		//用于产生随机字符串
		$this->load->helper('string');
		$register = $this->input->post();
		
		try{
			
			//验证邮箱地址格式是否正确
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			if ($this->form_validation->run() == FALSE)
			{
				throw new Exception('10001');
			}
			
			//验证昵称是否正确
			$this->form_validation->set_rules('nickname', 'nickname', 'required|max_length[10]');
			if ($this->form_validation->run() == FALSE)
			{
				throw new Exception('10010');
			}
			
			//验证昵称格式
			if(!preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\_]{1,}$/u", $register['nickname']))
			{
				throw new Exception('10010');
			}
			
			//验证密码是否正确
			$this->form_validation->set_rules('password', 'password', 'required|min_length[6]|max_length[16]');
			if ($this->form_validation->run() == FALSE)
			{
				throw new Exception('10003');
			}
			
			//验证密码2是否正确
			$this->form_validation->set_rules('passwd', 'passwd', 'required|min_length[6]|max_length[16]');
			if ($this->form_validation->run() == FALSE)
			{
				throw new Exception('10003');
			}
			
			//验证密码2和密码1是否一致
			$this->form_validation->set_rules('passwd', 'passwd', 'matches[password]');
			if ($this->form_validation->run() == FALSE)
			{
				throw new Exception('10004');
			}
			
			//验证邮箱是否已经被注册
			if( $this->m_user->selectUserByEmail($register['email']))
			{
				throw new Exception('10005');
			}
			
			//验证用户名是否已经被注册
			if( $this->m_user->selectUserByNickname($register['nickname']))
			{
				throw new Exception('10006');
			}

		}catch (Exception $e){
			echo $e->getMessage();
			exit;
		}
		
		//产生16位随机字符串
		$validCode = random_string('alnum', 16);
		
		$user_password = sha1(md5($register['password']).$validCode);
		
		$user_id = $this->m_user->insertUser($register['nickname'],$user_password,$register['email'],$validCode);
		
		if($user_id)
		{
			//记录用户详细数据到user_data表
			$this->m_user->insertUserData($user_id);
			
			$newdata = array('user_id'  => $user_id,'user_nickname' => $register['nickname']);
			
			$this->session->set_userdata($newdata);
			//发送邮件
			//$this->sendEmail($register['email']);
			
			echo '10000';
		}
		
	}
	
	public function sendEmail($email)
	{
		$this->load->library('email');
		$config['protocol'] = 'smtp';
		$config['smtp_host'] = 'smtp.163.com';
		$config['smtp_user'] = 'yanyinghq@163.com';
		$config['smtp_pass'] = 'gx1992x03y23hq';
		$this->email->initialize($config);
		$this->email->from('yanyinghq@163.com', 'Your Name');
		$this->email->to($email);
		$this->email->subject('Email Test');
		$this->email->message('Testing the email class.');
		$this->email->send();
	}
}

/* End of file Register.php */
/* Location: ./application/controllers/Register.php */